package com.yeswater.multithread.chapter6;

/**
 * author : yeswater
 * date : 2023/10/24
 */

public class Run_6_1_2 {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        MyThread t3 = new MyThread();

        t1.start();
        t2.start();
        t3.start();
    }

    private static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println(MyObject.getInstance().hashCode());
        }
    }

    private static class MyObject {
        private static MyObject myObject;

        private MyObject() {

        }

        /**
         * 多线程环境会出现取出多个实例的情况
         */
        public static MyObject getInstance() {
            if (myObject != null) {

            } else {
                myObject = new MyObject();
            }
            return myObject;
        }
    }
}
